<?php
class NoteModel extends RelationModel
{
	public $_link = array (

      'user_id' => array(   
 					'mapping_type'=>BELONGS_TO, 
                    'class_name'=>'user', 
                    'foreign_key'=>'user_id', 
                    'mapping_name'=>'user',
                   )       
	);
	
	
	public function addNote($book_id, $user_id, $title, $content) {
		$note['user_id'] = $user_id;
		$note['book_id'] = $book_id;
		$note['content'] = $content;
		$note['title'] = $title;
		$note['create_time'] = time();
		
		$this->add($note);
	}
	
	
	public function getByBookId($book_id) {
		$condition['book_id'] = $book_id;
		$notes = $this->where($condition)->relation(true)->select();
		return $notes;
	}
	
	public function getById($note_id) {
		$condition['id'] = $note_id;
		$note = $this->where($condition)->relation(true)->find(); 
		return $note;
	}
	
	public function getByUserId($user_id) {
		$condition['user_id'] = $user_id;
		$notes = $this->where($condition)->order("create_time desc")
			->select();
		return $notes;
	}
	
	public function getByUserIdAndPage($user_id, $record_num) {
		$condition['user_id'] = $user_id;
		import("ORG.Util.Page"); //  导入分页类 
	 	$count = $this->where($condition)->count();
	 	$Page = new Page ( $count, $record_num );
	 	$show = $Page->show (); //  分页显示输出 
	 	$notes = $this->where($condition)->relation(true)
			->order('create_time desc')
			->limit ( $Page->firstRow.','.$Page->listRows )->select();
		$result = array('data'=>$notes,'page'=>$show);
	 	return $result;
	}
	
	
	
}
?>